package cn.TS.servlet;

import cn.TS.tools.ConnDB;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("action");
        if ("register".equals(action)) {
            this.register(request, response);
        } else if ("login".equals(action)) {
            this.login(request, response);
        } else if ("exit".equals(action)) {
            this.exit(request, response);
        }
        this.doGet(request, response);
    }

    private void exit(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub

    }

    private void login(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub

    }

    private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String logname = request.getParameter("logname");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String message = "";
        String url = "";
        ConnDB conn = new ConnDB();
        String sql = "SELECT * FROM ts_user WHERE logname='" + logname + "";
        ResultSet rs = conn.executeQuety(sql);
        try {
            if (rs.next()) {
                message = "���û����ѱ�ע��";
                url = "show.jsp";
            } else {
                String sql_ins = "INSERT INTO ts_user(logname,password,email)VALUES('"
                        + logname + "'," + password + "','" + email + "')";
                int rtn = conn.executeUpdate(sql_ins);
                if (rtn > 0) {
                    message = "�û�ע��ɹ�";
                    url = "login.html";
                } else {
                    message = "�û�ע��ʧ��";
                    url = "show.jsp";
                }
            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            conn.close();
        }
        request.setAttribute("message", message);
        request.setAttribute("url", url);
        request.getRequestDispatcher("resources/register_ok.jsp").forward(request, response);
    }
}


